package io;

import java.io.*;
import java.util.Scanner;

public class IODemo12 {
    public static void main(String[] args) {
        // 输入两个路径
        // 分别是 从哪里 拷贝 到 哪里
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要拷贝哪个文件: ");
        String srcPath = scanner.next();
        System.out.println("请输入要拷贝到哪个地方: ");
        String destPath = scanner.next();

        File srcFile = new File(srcPath);
        if (!srcFile.isFile()) {
            System.out.println("您输入的源路径有误");
            return;
        }
        File destFile = new File(destPath);
        if (destFile.isFile()) {
            System.out.println("您输入的目标路径有误");
            return;
        }

        // 拷贝操作
        try (InputStream inputStream = new FileInputStream(srcFile);
             OutputStream outputStream = new FileOutputStream(destFile)) {
            // 进行读文件操作
            while (true) {
                int b = inputStream.read();
                if (b == -1) {
                    break;
                }
                outputStream.write(b);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
